package com.pet.system.util;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.pet.server.po.SysUser;
import com.pet.system.constants.ATTSystem;

public class SessionHelper {
	
	public static void remove(String name){
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();
		session.remove(name);
	}
	public static void invalidate() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		if(session!=null){
			session.invalidate();
		}
	}
	public static Integer getRoleId(){
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();
		Object obj = session.get(ATTSystem.SESSION_USER);
		if(null!=obj){
			if(obj.getClass().equals(com.pet.server.po.SysUser.class)){
				SysUser user = (SysUser)obj;
				return user.getRoleInfo().getRoleId();
			}	
		}
		return null;
	}

    public static SysUser getSysUser(){
    	return get(ATTSystem.SESSION_USER, SysUser.class);
    }
	public static <T> T get(String key, Class<T> toType) {
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();
		return (T)session.get(key);
	}
	public static HttpServletRequest getRequest(){
		return ServletActionContext.getRequest();
	}
	public static void set(String key, Object value) {
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();
		session.put(key, value);
	}
}
